<?php
/**
 * Created by PhpStorm.
 * User: xunuo
 * Date: 2017/6/27
 * Time: 13:22
 */

namespace app\admin\model;
use app\lib\enum\RefundStatusEnum;
use think\Model;

/**
 * Class Order
 * @package app\admin\model
 * 订单模型
 */
class Order extends Model
{
    /**
     * @param $pageNum
     * @param $numPerPage
     * @param $order_sn
     * @param $company_name
     * @param $order_status
     * @param $starttime
     * @param $stoptime
     * @param $mobile
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 订单列表 (停用)
     */
    // public function getList($pageNum, $numPerPage, $order_sn, $company_name, $order_status)
    // {
    //     $map = [];
    //     if (!empty($order_sn)) {
    //         $map['order_sn'] = ['like', "%{$order_sn}%"];
    //     }
    //     if ($order_status != '') {
    //         $map['order_status'] = $order_status;
    //     }
    //     if (!empty($company_name)) {
    //         $company_id = [];
    //         //根据商家名称搜索出商家id数组
    //         $company_id_array = db('company')
    //             ->field('id')
    //             ->where('company_name', 'like', "%{$company_name}%")
    //             ->select();
    //         foreach ($company_id_array as $val_company_id) {
    //             $company_id[] = $val_company_id['id'];
    //         }
    //         $map['company_id'] = ['in', $company_id];
    //     }
    //     $order_detail = db("order")
    //         ->where($map)
    //         ->page($pageNum, $numPerPage)
    //         ->order('create_time desc')
    //         ->select();
    //     foreach ($order_detail as $key => $value) {
    //         $company = db('company')->where('id', $value['company_id'])->find();
    //         $order_detail[$key]['company_name'] = $company['company_name'];
    //         $user = db('users')->where('open_id', $value['open_id'])->find();
    //         $user_name = db('driving_license')->where('order_id', $value['order_id'])->value('user_name');
    //         $order_detail[$key]['username'] = $user_name;
    //     }
    //     $data['list'] = $order_detail;
    //     $data['count'] = count($order_detail);
    //     return $data;
    // }

    /**
     * @Desc 订单列表(新)
     * @param array $map 条件
     * @param int $pageNum 页码
     * @param int $numPerPage 每页数量
     * @param mixed $count 是否为计算数量
     *
     * @return bool|false|int|\PDOStatement|string|\think\Collection|null
     */
    public function get_list($map, $pageNum, $numPerPage, $count = '')
    {
        $field = 'o.*, d.user_name, d.license_number';
        if (empty($count)) {
            $result = db('order')
                ->alias('o')
                ->join('driving_license d', 'd.id = o.driving_license_id')
                ->where($map)
                ->field($field)
                ->order('o.subscribe_date desc, o.create_time desc')
                ->page($pageNum, $numPerPage)
                ->select();
        } else {
            $result = db('order')
                ->alias('o')
                ->join('driving_license d', 'd.id = o.driving_license_id')
                ->where($map)
                ->count();
        }
        if(is_array($result)){
            foreach ($result as &$value) {
                $value['company_name'] = db('company')->where('id', $value['company_id'])->value('company_name');
            }
        }
        return $result;
    }

    /**
     * @param $order_sn
     * @param $company_name
     * @param $order_status
     * @param $starttime
     * @param $stoptime
     * @param $mobile
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 订单总数
     */
    public function getCount($order_sn, $company_name, $order_status)
    {
        $map = [];
        if (!empty($order_sn)) {
            $map['order_sn'] = ['like', "%{$order_sn}%"];
        }
        if ($order_status != '') {
            $map['order_status'] = $order_status;
        }
        if ($company_name != '') {
            $company_id = [];
            //根据商家名称搜索出商家id数组
            $cmopany_id_array = db('company')
                    ->field('id')
                    ->where('company_name', 'like', "%{$company_name}%")
                    ->select();
            foreach ($cmopany_id_array as $val_company_id) {
                $company_id[] = $val_company_id['id'];
            }
            $map['company_id'] = array('in', $company_id);
        }
        $count = db("order")
            ->field("id")
            ->where($map)
            ->count();
        return $count;
    }

    /**
     * @param $company_name
     * @param $zhuangtai
     * @param $order_status
     * @param $starttime
     * @param $stoptime
     *
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 订单统计-数量
     */
    public function getCountstatic($company_name, $zhuangtai, $order_status, $starttime, $stoptime, $isRefund = 0)
    {
        $map = [];
        if ($order_status != '') {
            $map['order_status'] = $order_status;
        }
        if ($company_name != '') {
            $company_id = [];
            //根据商家名称搜索出商家id数组
            $cmopany_id_array = db('company')
                ->field('id')
                ->where('company_name', 'like', "%{$company_name}%")
                ->select();
            foreach ($cmopany_id_array as $val_company_id) {
                $company_id[] = $val_company_id['id'];
            }
            $map['company_id'] = array('in', $company_id);
        }
        if (!empty($starttime) && !empty($stoptime)) {
            $map[$zhuangtai] = ['between', [strtotime($starttime), strtotime($stoptime)]];
        }
        if (!empty($starttime) && empty($stoptime)) {
            $map[$zhuangtai] = array('EGT', strtotime($starttime));
        }
        if (empty($starttime) && !empty($stoptime)) {
            $map[$zhuangtai] = array('ELT', strtotime($stoptime));
        }
        // 已退款
        if ($isRefund) {
            $map['refund_status'] = RefundStatusEnum::REFUNDED;
        }
        $count = db("order")
            ->field("id")
            ->where($map)
            ->count();
        return $count;
    }
    /**
     * @param $order_sn
     * @param $company_name
     * @param $order_status
     * @param $starttime
     * @param $stoptime
     * @param $mobile
     * @return float|int
     * 订单金额
     */
    public function getOrderJe($company_name, $zhuangtai, $order_status, $starttime, $stoptime, $isRefund = 0)
    {
        $map = [];
        if ($order_status != '') {
            $map['order_status'] = $order_status;
        }
        if ($company_name != '') {
            $company_id = [];
            //根据商家名称搜索出商家id数组
            $cmopany_id_array = db('company')
                ->field('id')
                ->where('company_name', 'like', "%{$company_name}%")
                ->select();
            foreach ($cmopany_id_array as $val_company_id) {
                $company_id[] = $val_company_id['id'];
            }
            $map['company_id'] = array('in', $company_id);
        }
        if (!empty($starttime) && !empty($stoptime)) {
            $map[$zhuangtai] = ['between', [strtotime($starttime), strtotime($stoptime)]];
        }
        if (!empty($starttime) && empty($stoptime)) {
            $map[$zhuangtai] = array('EGT', strtotime($starttime));
        }
        if (empty($starttime) && !empty($stoptime)) {
            $map[$zhuangtai] = array('ELT', strtotime($stoptime));
        }
        if ($isRefund) {
            $map['refund_status'] = RefundStatusEnum::REFUNDED;
        }
        $order_je = db("order")
            ->where($map)
            ->sum("order_amount");
        return $order_je;
    }
    /**
     * @param $settle_status
     * @param $company_name
     * @param $starttime
     * @param $stoptime
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 核销-数量
     */
    public function gethx($settle_status, $zhuangtai, $company_name, $starttime, $stoptime)
    {
        $map = [];
        $map['order_status'] = 3;
        if ($company_name != '') {
            $company_id = [];
            //根据商家名称搜索出商家id数组
            $cmopany_id_array = db('company')
                ->field('id')
                ->where('company_name', 'like', "%{$company_name}%")
                ->select();
            foreach ($cmopany_id_array as $val_company_id) {
                $company_id[] = $val_company_id['id'];
            }
            $map['company_id'] = array('in', $company_id);
        }
        if (!empty($starttime) && !empty($stoptime)) {
            $map[$zhuangtai] = ['between', [strtotime($starttime), strtotime($stoptime)]];
        }
        if (!empty($starttime) && empty($stoptime)) {
            $map[$zhuangtai] = array('EGT', strtotime($starttime));
        }
        if (empty($starttime) && !empty($stoptime)) {
            $map[$zhuangtai] = array('ELT', strtotime($stoptime));
        }
        $count = db("order")
            ->field("id")
            ->where($map)
            ->where("is_distribut",$settle_status)
            ->count();
        return $count;
    }

    /**
     * @param $settle_status
     * @param $company_name
     * @param $starttime
     * @param $stoptime
     * @return mixed
     * 核销-金额
     */
    public function getje($settle_status, $zhuangtai, $company_name, $starttime, $stoptime)
    {
        $map = [];
        $map['order_status'] = 3;
        if ($company_name != '') {
            $company_id = [];
            //根据商家名称搜索出商家id数组
            $cmopany_id_array = db('company')
                ->field('id')
                ->where('company_name', 'like', "%{$company_name}%")
                ->select();
            foreach ($cmopany_id_array as $val_company_id) {
                $company_id[] = $val_company_id['id'];
            }
            $map['company_id'] = array('in', $company_id);
        }
        if (!empty($starttime) && !empty($stoptime)) {
            $map[$zhuangtai] = ['between', [strtotime($starttime), strtotime($stoptime)]];
        }
        if (!empty($starttime) && empty($stoptime)) {
            $map[$zhuangtai] = array('EGT', strtotime($starttime));
        }
        if (empty($starttime) && !empty($stoptime)) {
            $map[$zhuangtai] = array('ELT', strtotime($stoptime));
        }
        $je = db("order")
            ->where($map)
            ->where("is_distribut",$settle_status)
            ->sum("order_amount");
        return $je;
    }
}